<template>
  <view :class="show?'slide':''">
    <view class="content">
      <image src="../../static/index/授权登录图@2x.png" mode="widthFix"></image>
      <div class="btn" @click="wxlogin">微信授权一键登录</div>
    </view>
  </view>
</template>

<script>
import Api from "../../API/api"
import request from '../../API/request.js'
import PathValue from "../../utils/pathValue";

export default {
  data() {
    return {
      show: false
    }
  },
  onShow() {
    //预加载主页
    uni.preloadPage({
      url: "/pages/index/index"
    })
    uni.preloadPage({
      url: "/pages/user/index"
    })
    let uid = PathValue.getUrlValue("uid");
    if (uid) {
      this.show = true
      setTimeout(() => {
        this.show = false
        uni.redirectTo({
          url: "/pages/user/index?uid=" + uid
        })
      }, 200)
      return;
    }
    //判断token
    const a = uni.getStorageSync("token");
    if (a) {
      this.show = true
      setTimeout(() => {
        this.show = false
        uni.redirectTo({
          url: "/pages/index/index"
        })
      }, 200)
      return;
    }
    //获取code
    let code = PathValue.getUrlValue("code");
    let acting = PathValue.getUrlValue("state");
    //使用code自动授权
    if (code) {
      Api.accessToken({"acting": acting, "code": code, "kind": true}).then(res => {
        uni.setStorageSync("token", res.data.accessToken)
        if (res.code === 0) {
          this.show = true
          setTimeout(() => {
            this.show = false
            uni.redirectTo({
              url: "/pages/index/index"
            })
          }, 200)
          setTimeout(() => {
            uni.showToast({
              title: "登录成功",
              icon: 'checkmarkempty'
            })
          }, 300);
        }
      })
    }
    uni.hideLoading();
  },
  // onLoad() {
  // 	//预加载主页
  // 	uni.preloadPage({
  // 		url: "/pages/index/index"
  // 	})
  // 	uni.preloadPage({
  // 		url: "/pages/user/index"
  // 	})
  // 	let uid = PathValue.getUrlValue("uid");
  // 	if (uid) {
  // 		uni.redirectTo({
  // 			url: "/pages/user/index"
  // 		})
  // 		return;
  // 	}
  // 	//判断token
  // 	const a = uni.getStorageSync("token");
  // 	if (a) {
  // 		uni.redirectTo({
  // 			url: "/pages/index/index"
  // 		})
  // 		return;
  // 	}
  // 	//获取code
  // 	let code = PathValue.getUrlValue("code");
  // 	let agentId = PathValue.getUrlValue("state");
  // 	//使用code自动授权
  // 	if (code) {
  // 		Api.accessToken(code, agentId).then(res => {
  // 			uni.setStorageSync("token", res.data)
  // 			if (res.code === 0) {
  // 				uni.redirectTo({
  // 					url: "/pages/index/index"
  // 				})
  // 			}
  // 		})
  // 	}
  //   uni.hideLoading();
  // },
  methods: {
    login() {
      //手机登录
      uni.redirectTo({
        url: '/pages/login/login_phone'
      })
    },
    wxlogin() {
      uni.showLoading({
        title: '加载中'
      });
      setTimeout(function () {
        uni.hideLoading();
      }, 3000);
      setTimeout(() => {
        uni.showToast({
          title: res.message,
          icon: 'checkmarkempty'
        })
      }, 1000);
      let acting = PathValue.getUrlValue("acting");
      console.log(acting)
      //TODO: agentId = 代理  , userId = 用户
      window.location.href = request.BASE_URL + '/api/wap/move/login/login/?acting='+ acting;
    },
  }
}
</script>

<style lang="less" scoped>
page {
  background-color: #fff;
}

// 滑动动画
.slide {
  transform: translateX(-750rpx);
  transition: 0.2s;
}

.content {
  display: flex;
  justify-content: center;
  align-items: center;

  image {
    margin-top: 100rpx;
  }

  .btn {
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed;
    // bottom: 50rpx;
    top: 65%;
    left: 50%;
    transform: translateX(-50%);
    width: 520rpx;
    height: 96rpx;
    background: linear-gradient(180deg, #FFBE00FF 0%, #FFC200FF 100%);
    border-radius: 48rpx;
    color: #fff;
  }
}
</style>